//
//  HomeTabBarController.m
//  项目一TimeMovie
//
//  Created by apple on 16/3/29.
//  Copyright © 2016年 huangmingyu. All rights reserved.
//

#import "HomeTabBarController.h"
#import "TabBarButton.h"

@interface HomeTabBarController ()  {
    
    UIImageView *_seclectImage;
}

@end

@implementation HomeTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self customTabBar];
    [self creatStoryBroad];
    
    
//    NSLog(@"%@",self.tabBar.subviews);
}

// 清除原来的buttonItem
- (void)clearTabBarButton {
    
    for (UIView *view in self.tabBar.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            [view removeFromSuperview];
        
        }
    }
    
    
}


- (void)creatStoryBroad {
    
    UIStoryboard *home = [UIStoryboard storyboardWithName:@"Home" bundle:nil];
    UIStoryboard *news = [UIStoryboard storyboardWithName:@"News" bundle:nil];
    UIStoryboard *Top = [UIStoryboard storyboardWithName:@"Top" bundle:nil];
    UIStoryboard *cinema = [UIStoryboard storyboardWithName:@"Cinema" bundle:nil];
    UIStoryboard *more = [UIStoryboard storyboardWithName:@"More" bundle:nil];
    
    NSArray *array = @[
                       [home instantiateInitialViewController],
                       [news instantiateInitialViewController],
                       [Top instantiateInitialViewController],
                       [cinema instantiateInitialViewController],
                       [more instantiateInitialViewController]
                       ];
    self.viewControllers = array;
    
}

// 自定义标签栏
- (void)customTabBar {
    
    //标签栏背景图片
    self.tabBar.backgroundImage = [UIImage imageNamed:@"tab_bg_all@2x"];
    
    //选中时的图片
    _seclectImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"selectTabbar_bg_all1"]];
    _seclectImage.frame = CGRectMake(0, 0, 49, 49);
    [self.tabBar addSubview:_seclectImage];
    
   
    
    //获取标签栏按钮的名字和背景图片的名字
    NSArray *nameArr = @[@"电影",@"新闻",@"TOP",@"影院",@"更多"];
    NSArray *imageArr = @[
                          @"movie_home@2x",
                          @"msg_new@2x",
                          @"start_top250@2x",
                          @"icon_cinema@2x",
                          @"more_setting@2x"
                          ];
    
     //设置标签栏按钮
    for (int i = 0; i <nameArr.count; i++) {
        
        TabBarButton *tabBarButton = [[TabBarButton alloc] initWithFrame:CGRectMake(i * (kScreenWidth / 5), 0, kScreenWidth / 5, 49) withImageName:imageArr[i] tiltleName:nameArr[i]];
        tabBarButton.tag = 1000 + i;
        [tabBarButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:tabBarButton];
    }
    
    _seclectImage.center = [self.tabBar viewWithTag:1000].center;
}

- (void)buttonAction:(TabBarButton *)button {
    
    self.selectedIndex = button.tag - 1000;
    
    [UIView animateWithDuration:0.25 animations:^{
       
        _seclectImage.center = button.center;
    }];
    
    
}



- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:YES];
    
    [self clearTabBarButton];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
